// import { Message, Loading, MessageBox } from 'element-ui';
import { ElMessage as Message, ElLoading as Loading,ElMessageBox as MessageBox } from 'element-plus'

// element-ui/Message-消息提示封装
export function msgFactory(msg, flag) {
  // top-right/top-left/bottom-right/bottom-left
  if (flag !== 0 && !flag) {
    flag = 99;
  }
  
  switch (flag) {
    case 0:
      flag = 'success';
      break;
    case 1:
      flag = 'warning';
      break;
    case 2:
      flag = 'info';
      break;
    case 3:
      flag = 'error';
      break;
    default:
      flag = 'info';
  }

  Message({
    duration: flag === 'error' ? 5000 : 3000,
    message: msg,
    type: flag,
    // position: pos,
    offset: 80
  });
}

export function msgBoxFactory(title, msg, options) {
  if (options.delay) {
    options.showCof = false;
    options.showCal = false;
  }
  MessageBox({
    title: title,
    type: options.type,
    message: msg,
    // 是否可通过点击遮罩关闭 MessageBox
    closeOnClickModal: false,
    // 是否可通过按下 ESC 键关闭 MessageBox
    closeOnPressEscape: false,
    // 是否显示关闭按钮
    showClose: false,
    // 是否将 message 属性作为 HTML 片段处理
    dangerouslyUseHTMLString: true,
    // 是否显示取消按钮
    showCancelButton: options.showCal !== undefined ? options.showCal : false,
    // 是否显示确定按钮
    showConfirmButton: options.showCof !== undefined ? options.showCof : true,
    // 取消按钮的文本内容
    cancelButtonText: options.textCal ? options.textCal : '取消',
    // 确定按钮的文本内容
    confirmButtonText: options.textCof ? options.textCof : '确认'
  }).then(() => {
    if (options.confirm) {
      options.confirm();
    }
  }).catch(() => {
    if (options.cancel) {
      options.cancel();
    }
  });

  if (options.delay) {
    setTimeout(() => {
      MessageBox.close();
      if (options.confirm) {
        options.confirm();
      }
    }, options.delay);
  }
}

// element-ui/Message-消息提示封装
export const _tiper = {
  error: function(msg) {
    msgFactory(msg, 3);
  },
  warn: function(msg) {
    msgFactory(msg, 1);
  },
  info: function(msg) {
    msgFactory(msg, 2);
  },
  success: function(msg) {
    msgFactory(msg, 0);
  },

  load: function(msg, el) {
    return LOAD(msg, el);
  },
  confirm: function(msg, cbEvent, title) {
    confirmFactory(msg, cbEvent, title);
  },
  errorBox: function(title, msg, options) {
    options = options == null ? {} : options;
    options.type = 'error'
    msgBoxFactory(title, msg, options);
  },
  warnBox: function(title, msg, options) {
    options = options == null ? {} : options;
    options.type = 'warning'
    msgBoxFactory(title, msg, options);
  },
  infoBox: function(title, msg, options) {
    options = options == null ? {} : options;
    options.type = 'info'
    msgBoxFactory(title, msg, options);
  },
  successBox: function(title, msg, options) {
    options = options == null ? {} : options;
    options.type = 'success'
    msgBoxFactory(title, msg, options);
  }
};

// element-ui/load-加载提示封装
export function LOAD(msg, el) {
  const LOAD = Loading.service({
    target: el || document.body,
    lock: true,
    text: msg || '加载中...'
  });

  setTimeout(() => {
    // 3分钟延时自动关闭
    LOAD.close();
  }, 180000);

  return LOAD;
}

// element-ui/MessageBox-加载提示封装
export function confirmFactory(msg, cbEvent, title) {
  MessageBox.confirm(msg, title || '确认信息', {
    distinguishCancelAndClose: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      if(cbEvent){
        cbEvent(true);
      }
    })
    .catch((action) => {
      if(cbEvent){
        cbEvent(false);
      }
    });
}

/**
 * 动态加载iframe，解决ie问题
 * @param {Object} targetEl vue属性
 * @param {String} url 目标链接
 */
export function loadFrame(url) {
  const newIframeObject = document.createElement('IFRAME');
  newIframeObject.src = url;
  newIframeObject.scrolling = 'yes';
  newIframeObject.frameBorder = 0;
  newIframeObject.width = '100%';
  newIframeObject.height = '900px';
  return newIframeObject.outerHTML;
}

/**
 * 优化别名导出
 */
export const $tip = {
  ..._tiper,
  load: LOAD
}